<?php
namespace app\admin\controller;
use think\Controller;
class Base extends Controller
{
	public function initialize(){
		if(!session('username')){
            $this->redirect(url('login/login'));
        }
	}
    public function empty_list($num = 10){
        $empty = "<tr><td colspan='".$num."' align='center'>暂无数据</td></tr>";
        return $empty;
    }
    public function format_bytes($size, $delimiter = '') {
        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
        for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
        return round($size, 2) . $delimiter . $units[$i];
    }
	// 原图上传
    public function uploadtmd(){
        $photo='';
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath)){
            mkdir($imgpath,0777);
        }
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777);
        }
        chmod($imgpath.$imgsubpath,0777);
        if(isset($_FILES['file'])){
            move_uploaded_file($_FILES['file']['tmp_name'],$imgpath.$imgsubpath.$_FILES['file']['name']);
            $photo="/uploads/images/".$imgsubpath.$_FILES['file']['name'];
        }
        return json(['code' => 200, 'photo' =>$photo, 'msg' => '成功']);
    }
    // 原图上传
    public function uploadtmds(){
        $photo='';
        $imgs=[];
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath)){
            mkdir($imgpath,0777);
        }
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777);
        }
        chmod($imgpath.$imgsubpath,0777);
        if(isset($_FILES['file'])){
            move_uploaded_file($_FILES['file']['tmp_name'],$imgpath.$imgsubpath.$_FILES['file']['name']);
            $photo="/uploads/images/".$imgsubpath.$_FILES['file']['name'];
        }
        if(isset($_FILES['imgs'])){
            for ($i=0; $i <count($_FILES['imgs']['name']) ; $i++) {
                move_uploaded_file($_FILES['imgs']['tmp_name'][$i],$imgpath.$imgsubpath.$_FILES['imgs']['name'][$i]);
                $imgs[$i]="/uploads/images/".$imgsubpath.$_FILES['imgs']['name'][$i];
            }
        }
        return json(['code' => 200, 'photo' =>$photo,'imgs' =>implode(",",$imgs), 'msg' => '成功']);
    }
}